﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using MVCPets.Data;
using MVCPets.Data.Model;

namespace MVCPets.Models
{
  public class PetStatusModel
  {
    #region Public Properties
    public int? ID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    #endregion

    #region Constructors
    public PetStatusModel()
    {

    }

    public PetStatusModel(int id)
    {
			PetStatus status = Data.DataAccess.DataStore.LoadObject<PetStatus>(id);
			this.ID = status.ID;
			this.Name = status.StatusName;
			this.Description = status.StatusDescription;
    }    
    #endregion    

    #region Static Methods
    private static IList<PetStatus> GetStatusList()
    {
      return Data.DataAccess.DataStore.LoadEntireTable<PetStatus>();
    }

    public static PetStatus GetStatus(int id)
    {
      return GetStatusList().Where(R => R.ID == id).FirstOrDefault();
    }

    public static PetStatus GetStatus(string name)
    {
      return GetStatusList().Where(R => R.StatusName == name).FirstOrDefault();
    }

    public static List<PetStatusModel> GetStatuses()
    {
			List<PetStatusModel> ret = new List<PetStatusModel>();
			foreach (var v in GetStatusList())
				ret.Add(new PetStatusModel() { ID = v.ID, Name = v.StatusName, Description = v.StatusDescription });

      return ret;
    }
    #endregion

    #region Public Methods
    public bool Save()
    {
      bool ret = false;

      if (this.ID.HasValue) ret = Update();
      else ret = Insert();

      return ret;
    }

    public bool Delete()
    {
			return Data.DataAccess.DataStore.DeleteObject<PetStatus>(this.ID);
    }
    #endregion

    #region Private Methods
    private bool Insert()
    {
			return Data.DataAccess.DataStore.InsertObject(new PetStatus()
				{
					StatusName = this.Name,
					StatusDescription = this.Description
				});
    }

    private bool Update()
    {
			return Data.DataAccess.DataStore.UpdateObject(new PetStatus()
			{
				ID = this.ID.Value,
				StatusName = this.Name,
				StatusDescription = this.Description
			});
    }
    #endregion
  }
}